package com.song.controller;

import com.github.pagehelper.PageInfo;
import com.song.pojo.Player;
import com.song.pojo.PlayerQueryVO;
import com.song.pojo.ResultVO;
import com.song.service.PlayerService;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**球员核心层
 * @author song
 */
@Controller
@ResponseBody
@RequestMapping("player")
public class PlayerController {
    @Resource
    private PlayerService playerService;

    //Date类型转换
    @InitBinder
    protected void initDateFormatBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat,true));
    }

    @RequestMapping(value = "list",method = RequestMethod.GET)
    public ResultVO<Player> queryPlayers(Integer pageNum, Integer pageSize, PlayerQueryVO vo){
        System.out.println(vo);
        PageInfo<Player> pageInfo = playerService.queryPlayers(pageNum, pageSize, vo);
        return new ResultVO<>(pageInfo);
    }

    @RequestMapping(method = RequestMethod.POST)
    public ResultVO insertPlayer(Player player){
        System.out.println(player);
        Integer integer = playerService.insertPlayer(player);
        if (integer == 1){
            return new ResultVO<>();
        }
        return new ResultVO<>(500,"网络弹簧，请稍后进行");
    }

    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public ResultVO<Player> queryByPlayerId(@PathVariable("id")Integer playerId){
        Player player = playerService.queryByPlayerId(playerId);
        if (player != null){
            return new ResultVO<>(player);
        }
        return new ResultVO<>(500,"网络崩溃，稍后再查询");
    }

    @RequestMapping(value = "{id}",method = RequestMethod.PUT)
    public ResultVO updatePlayer(@PathVariable("id")Integer playerId,Player player){
        player.setPlayerId(playerId);
        Integer integer = playerService.updatePlayer(player);
        if (integer == 1){
            return new ResultVO<>();
        }
        return new ResultVO<>(500,"网络弹簧，请稍后进行");
    }

    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public ResultVO deletePlayer(@PathVariable("id")Integer playerId){
        Integer integer = playerService.deletePlayer(playerId);
        if (integer == 1){
            return new ResultVO<>();
        }else{
            return new ResultVO<>(500,"网络弹簧，请稍后进行");
        }
    }
}
